package com.concerneyes.rentbarsecond.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

/**
 * Depiction:
 * <p>
 * Modify:
 * <p>
 * Author: Kevin Lynn
 * <p>
 * Create Date：2016年11月16日 上午10:25:55
 * <p>
 * 
 * @version 1.0
 * @since 1.0
 */
class DBRentBarHelper extends OrmLiteSqliteOpenHelper {
	private final static int VERSION = 1;
	private final static String DB_NAME = "rent_bar.db";

	private static DBRentBarHelper instance;

	/**
	 * 单例获取该Helper
	 * 
	 * @param context
	 * @return
	 */
	public static synchronized DBRentBarHelper getHelper(Context context) {
		if (instance == null) {
			synchronized (DBRentBarHelper.class) {
				if (instance == null) {
					instance = new DBRentBarHelper(context);
				}
			}
		}

		return instance;
	}

	/**
	 * 释放资源
	 */
	@Override
	public void close() {
		super.close();
	}

	public DBRentBarHelper(Context context) {
		super(context, DB_NAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, GlassesDetailModelForDao.class);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
		try {
			if (newVersion > oldVersion) {
				TableUtils.dropTable(connectionSource, GlassesDetailModelForDao.class, true);
				onCreate(database, connectionSource);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
